var url = null;

$(function() {
	loadZone();
	loadType();
	loadStatus();
});

function loadZone() {
	$('#zone_query').combobox({
		url : getHttpRequestPath("/baseInfo/loadZone"),
		valueField : 'value',
		textField : 'name'
	});
}

function loadType() {
	$('#type_query').combobox({
		url : getHttpRequestPath("/transport/loadWayType"),
		valueField : 'value',
		textField : 'name'
	});
}

function loadStatus() {
	$('#status_query').combobox({
		url : getHttpRequestPath("/transport/loadCarStatus"),
		valueField : 'value',
		textField : 'name'
	});
}

function loadZone2() {
	$('#zone_text').combobox({
		url : getHttpRequestPath("/baseInfo/loadZone"),
		valueField : 'value',
		textField : 'name'
	});
}

function loadType2() {
	$('#type_text').combobox({
		url : getHttpRequestPath("/transport/loadWayType"),
		valueField : 'value',
		textField : 'name'
	});
}

/**
 * 查询
 */
function query() {
	var cmd = new Cmd();
	var carNum = $("#number_query").val();
	cmd.carNum = carNum.length > 0 ? carNum : null;
	var zoneCode = $("#zone_query").combobox('getValue');
	cmd.zoneCode = zoneCode.length > 0 ? new Number(zoneCode) : 0;
	var type = $("#type_query").combobox('getValue');
	cmd.type = type.length > 0 ? type : null;
	var statusCode = $("#status_query").combobox('getValue');
	cmd.statusCode = statusCode.length > 0 ? new Number(statusCode) : 0;
	
	$('#dg').datagrid({
		url : getHttpRequestPath("/transport/queryCarsList"),
		pageNumber : 1,
		queryParams : {
			cmd : JSON.stringify(cmd, cmd.StringifyReplacer)
		}
	});
}

/**
 * 增加路线
 */
function initAdd() {
	url = getHttpRequestPath("/transport/addCar");
	clearData();
	loadZone2();
	loadType2();
	$('#dlg').dialog({
		modal : true
	}).dialog('open').dialog('setTitle', '增加车辆');
}

/**
 * 修改路线
 */
function initEdit(carNum) {
	url = getHttpRequestPath("/transport/editCar");
	clearData();
	getCar(carNum);
	loadZone2();
	loadType2();
	$('#dlg').dialog({
		modal : true
	}).dialog('open').dialog('setTitle', '修改车辆');
}

function clearData() {
	$('#number_text').val("");
	$('#zone_text').combobox("setValue", "");
	$('#type_text').combobox("setValue", "");
	$('#status_text').numberbox("setValue", "");
}

/**
 * 初始化表单数据
 * 
 * @param row
 */
function initCar(car) {
	$('#number_text').val(car.carNum);
	$('#zone_text').combobox("setValue", car.zoneCode);
	$('#type_text').combobox("setValue", car.type);
}

function formatZone(val, row, index) {
	var str = "";
	$.ajax({
		url:getHttpRequestPath("/baseInfo/getZone"),
		data:{
			"zoneCode":row.zoneCode,
		},
		success:function(response) {
			var zone = JSON.parse(response, function(key, value) {
				return value;
			});
			str += zone.country + "-" + zone.province + "-" + zone.city + "<br>";
		},
		async:false
	});
	return str;
}

function formatStatus(val, row, index) {
	var str = "";
	str += row.status;
	return str;
}

/**
 * 操作
 */
function formatAction(val, row, index) {
	var str = "";
	str += createLinkElement('修改', 'initEdit(\'' + row.carNum + '\')', 'icon-edit');
	str += createLinkElement('删除', 'delCar(\'' + row.carNum + '\')', 'icon-cancel');
	return str;
}
